#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
	int t;
	cin >> t;
	while (t--) {
		int l, r;
		cin >> l >> r;
		int ans = 0;
		for (int i = l; i <= r; i++) {
			string s = to_string(i);
			if (i % 13 == 0 && s.find("13") != -1)
				ans++;
		}
		cout << ans << endl;
	}
	return 0;
}
